package com.spelet.simulation;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import com.spelet.StaticSprites;
import com.spelet.VariableCollection;


public class TerrainBlock {


	public int type;
	public float spriteX;
	public float spriteY;

	private Sprite spriteToDraw;

	private int animStep;
	private int animSpeed;
	private int animTimer;
	public int dirtTimer;
	private int grassTimer;
	public int currentState;
	Sprite[] pillar; 
	Sprite[] button; 
	public int state;
	boolean hasFlower;
	public int tileX;
	public int tileY;
	//flowersettings if grass
	public int fxPos;
	public int fyPos;
	boolean filled;
	int animStep2;
	public boolean up;
	public boolean boulderoverdirt;
	public TerrainBlock(int type, int x, int y){
		this.type = type;
		spriteX = x*64;
		spriteY = y*64;

		tileX = x;
		tileY = y;
		animStep = 0;
		if(type == VariableCollection.grass){
			if (MathUtils.random(10) < 2) {
				hasFlower = true;
				fxPos = MathUtils.random(0, 64)+x*64;
				fyPos = MathUtils.random(0, 64)+y*64;
			}
			spriteToDraw = StaticSprites.grass[0];
		} else 	if(type == VariableCollection.hole){
			animSpeed = 100;
			state = VariableCollection.activated;
			animStep = 0;
			spriteToDraw = StaticSprites.holeAnim[0];
		} else if (type == VariableCollection.dirt) {
			spriteToDraw = StaticSprites.dirtAnim[0];
		} else if (type == VariableCollection.thefilledhole) {
			spriteToDraw = StaticSprites.fillHole[6];
		} else if (type == VariableCollection.button){
			button = StaticSprites.buttonpressed;
			spriteToDraw = button[4];
			up = true;
		} else if (type == VariableCollection.button_blue){
			button = StaticSprites.bluebuttonpressed;
			spriteToDraw = button[4];
			up = true;
		} else if(type == VariableCollection.pillar_yellow){
			pillar = StaticSprites.pillar;
			spriteToDraw = pillar[3];
			state = VariableCollection.pillarup;
			up = true;
		} else if(type == VariableCollection.pillar_blue){
			pillar = StaticSprites.bpillar;
			spriteToDraw = pillar[3];
			state = VariableCollection.pillarup;
			up = true;
		}


	}

	public void update(Simulation sim){
		animTimer += Gdx.graphics.getDeltaTime()*1000;
		animStep2 += Gdx.graphics.getDeltaTime()*1000;
		if (type == VariableCollection.button){
			if ((sim.map.blocks[tileX][tileY].type == VariableCollection.nothing || sim.map.blocks[tileX][tileY].type == VariableCollection.mergeblock) && !(sim.character.tileX == tileX && sim.character.tileY == tileY) ) {
				if(!up){
					state = VariableCollection.pushed;
				}
			} else {
				if(up){
					state = VariableCollection.pushed;
				}
			}
		} else if (type == VariableCollection.button_blue){
			if ((sim.map.blocks[tileX][tileY].type == VariableCollection.nothing || sim.map.blocks[tileX][tileY].type == VariableCollection.mergeblock) && !(sim.character.tileX == tileX && sim.character.tileY == tileY) ) {
				if(!up){
					state = VariableCollection.pushed;
				}
			} else {
				if(up){
					state = VariableCollection.pushed;
				}
			}
		} else if( type == VariableCollection.dirt){
			if (state == VariableCollection.inactive) { 
				dirtTimer += Gdx.graphics.getDeltaTime()*2000;
				int step = 900;
				if (dirtTimer < step)
					spriteToDraw = StaticSprites.dirt[0];
				else if (dirtTimer < step*2)
					spriteToDraw = StaticSprites.dirt[1];
				else if (dirtTimer < step*3)
					spriteToDraw = StaticSprites.dirt[2];
				else if (dirtTimer < step*4)
					spriteToDraw = StaticSprites.dirt[3];
				else{
					dirtTimer = 0;
				}
			} else if(state == VariableCollection.dirtboulder){
				dirtTimer += Gdx.graphics.getDeltaTime()*1000;
				int step = 150;
				if (dirtTimer < step)
					spriteToDraw = StaticSprites.dirtboulder[0];
				else if (dirtTimer < step*2)
					spriteToDraw = StaticSprites.dirtboulder[1];
				else if (dirtTimer < step*3)
					spriteToDraw = StaticSprites.dirtboulder[2];
				else if (dirtTimer < step*4)
					spriteToDraw = StaticSprites.dirtboulder[3];
				else if (dirtTimer < step*5)
					spriteToDraw = StaticSprites.dirtboulder[4];
				else if (dirtTimer < step*6)
					spriteToDraw = StaticSprites.dirtboulder[5];
				else{
					dirtTimer = 0;
					state = VariableCollection.inactive;
				}
			}  else if(state == VariableCollection.dirtboulderright){
				dirtTimer += Gdx.graphics.getDeltaTime()*1000;
				int step = 150;
				if (dirtTimer < step)
					spriteToDraw = StaticSprites.dirtboulderright[0];
				else if (dirtTimer < step*2)
					spriteToDraw = StaticSprites.dirtboulderright[1];
				else if (dirtTimer < step*3)
					spriteToDraw = StaticSprites.dirtboulderright[2];
				else if (dirtTimer < step*4)
					spriteToDraw = StaticSprites.dirtboulderright[3];
				else{
					dirtTimer = 0;
					state = VariableCollection.inactive;
				}
			}  else if(state == VariableCollection.dirtboulderdown){
				dirtTimer += Gdx.graphics.getDeltaTime()*1000;
				int step = 150;
				if (dirtTimer < step)
					spriteToDraw = StaticSprites.dirtboulderdown[0];
				else if (dirtTimer < step*2)
					spriteToDraw = StaticSprites.dirtboulderdown[1];
				else if (dirtTimer < step*3)
					spriteToDraw = StaticSprites.dirtboulderdown[2];
				else{
					dirtTimer = 0;
					state = VariableCollection.inactive;
				}
			} else if( state == VariableCollection.dirtboulderup){
				dirtTimer += Gdx.graphics.getDeltaTime()*1000;
				int step = 150;
				if (dirtTimer < step)
					spriteToDraw = StaticSprites.dirtboulderup[0];
				else if (dirtTimer < step*2)
					spriteToDraw = StaticSprites.dirtboulderup[1];
				else if (dirtTimer < step*3)
					spriteToDraw = StaticSprites.dirtboulderup[2];
				else if (dirtTimer < step*4)
					spriteToDraw = StaticSprites.dirtboulderup[3];
				else{
					dirtTimer = 0;
					state = VariableCollection.inactive;
				}
			}
		} else if( type == VariableCollection.grass){
			if(MathUtils.random(100) < 4){
				grassTimer += Gdx.graphics.getDeltaTime()*2000;
				int step = 900;
				if (grassTimer < step)
					spriteToDraw = StaticSprites.grass[0];
				else if (grassTimer < step*2)
					spriteToDraw = StaticSprites.grass[1];
				else if (grassTimer < step*3)
					spriteToDraw = StaticSprites.grass[2];
				else{
					grassTimer = 0;
				}
			}
		} else if (type == VariableCollection.pillar_yellow) {
			if (sim.map.blocks[tileX][tileY].type == VariableCollection.moveBlock) {
				spriteToDraw = pillar[0];
			} else if (sim.character.tileX == tileX && sim.character.tileY == tileY && !up) {
				spriteToDraw = pillar[0];
			} else if (sim.character.tileX == tileX && sim.character.tileY == tileY && up) {
				spriteToDraw = pillar[3];
			} else if (sim.map.blocks[tileX][tileY].type == VariableCollection.nothing && up) {
				spriteToDraw = pillar[3];
			}
		}else if (type == VariableCollection.pillar_blue) {
			if (sim.map.blocks[tileX][tileY].type == VariableCollection.moveBlock) {
				spriteToDraw = pillar[0];
			} else if (sim.character.tileX == tileX && sim.character.tileY == tileY && !up) {
				spriteToDraw = pillar[0];
			} else if (sim.character.tileX == tileX && sim.character.tileY == tileY && up) {
				spriteToDraw = pillar[3];
			} else if (sim.map.blocks[tileX][tileY].type == VariableCollection.nothing && up) {
				spriteToDraw = pillar[3];
			}
		} else if (type == VariableCollection.dirtStuck) {
			spriteToDraw = StaticSprites.dirtAnim[1];
		} else if (type == VariableCollection.hole) {
			if (state == VariableCollection.fillingHole) {
				int step = 120;
				animStep2 += Gdx.graphics.getDeltaTime()*1000;
				if (animStep2 < step)
					spriteToDraw = StaticSprites.fillHole[0];
				else if (animStep2 < step*2)
					spriteToDraw = StaticSprites.fillHole[1];
				else if (animStep2 < step*3)
					spriteToDraw = StaticSprites.fillHole[2];
				else if (animStep2 < step*4)
					spriteToDraw = StaticSprites.fillHole[3];
				else if (animStep2 < step*5)
					spriteToDraw = StaticSprites.fillHole[4];
				else if (animStep2 < step*6)
					spriteToDraw = StaticSprites.fillHole[5];
				else {
					type = VariableCollection.thefilledhole;
					filled = true;
					spriteToDraw = StaticSprites.fillHole[6];
					state = VariableCollection.inactive;	
				}
			}
		} 
		if (type == VariableCollection.pillar_blue || type == VariableCollection.pillar_yellow){ 
			if (state == VariableCollection.pillarMdown) {
				int step = 80;
				animStep2 += Gdx.graphics.getDeltaTime()*1000;
				if (animStep2 <= step) {
					spriteToDraw = pillar[3];
				} else if (animStep2 <= step*2){
					spriteToDraw = pillar[2];
				} else if (animStep2 <= step*3){
					spriteToDraw = pillar[1];
				}
				else {
					spriteToDraw = pillar[0];
					state = VariableCollection.pillardown;
					up = false;
				}
			} else if (state == VariableCollection.pillarMup) {
				int step = 80;
				up = true;
				if (sim.map.blocks[tileX][tileY].type != VariableCollection.nothing || (sim.character.tileX == tileX && sim.character.tileY == tileY)) {
					spriteToDraw = pillar[0];
					up = false;
					animStep2 = -400;
					return;
				} else {
					animStep2 += Gdx.graphics.getDeltaTime()*1000;
				}
				if (animStep2 <= step) {
					spriteToDraw = pillar[0];
				} else if (animStep2 <= step*2){
					spriteToDraw = pillar[1];
				} else if (animStep2 <= step*3){
					spriteToDraw = pillar[2];
				} else {
					spriteToDraw = pillar[3];
					state = VariableCollection.pillarup;
				}
			}
		} 
		if (type == VariableCollection.button || type == VariableCollection.button_blue) {
			if(state == VariableCollection.pushed){
				if(!up){
					int step = 60;
					animStep2 += Gdx.graphics.getDeltaTime()*1000;
					if (animStep2 < step)
						spriteToDraw = button[0];
					else if (animStep2 < step*2)
						spriteToDraw = button[1];
					else if (animStep2 < step*3)
						spriteToDraw = button[2];
					else if (animStep2 < step*4)
						spriteToDraw = button[3];
					else {
						spriteToDraw = button[4];
						up = true;
						state = VariableCollection.inactive;
						if(type == VariableCollection.button){
							boolean ok=true;
							for (int i = 0; i < sim.map.yellowpillars.size; i++)
								if (sim.map.yellowpillars.get(i).up)
									ok = false;
							if (ok) {
								for(int i =0; i < sim.map.yellowpillars.size; i++){
									sim.map.yellowpillars.get(i).state = VariableCollection.pillarMup;
									sim.map.yellowpillars.get(i).activate();
								}
							}
						}else if( type == VariableCollection.button_blue){
							boolean ok=true;
							for (int i = 0; i < sim.map.bluepillars.size; i++)
								if (sim.map.bluepillars.get(i).up)
									ok = false;
							if (ok) {
								for(int i =0; i < sim.map.bluepillars.size; i++){
									sim.map.bluepillars.get(i).state = VariableCollection.pillarMup;
									sim.map.bluepillars.get(i).activate();
								}
							}
						}
					}
				} else {
					int step = 60;
					animStep2 += Gdx.graphics.getDeltaTime()*1000;
					if (animStep2 < step)
						spriteToDraw = button[4];
					else if (animStep2 < step*2)
						spriteToDraw = button[3];
					else if (animStep2 < step*3)
						spriteToDraw = button[2];
					else if (animStep2 < step*4)
						spriteToDraw = button[1];
					else {
						spriteToDraw = button[0];
						up = false;
						state = VariableCollection.inactive;
						if(type == VariableCollection.button) {
							boolean ok = true;
							for (int i = 0; i < sim.map.yellowbuttons.size; i++)
								if (sim.map.yellowbuttons.get(i).up)
									ok = false;
							if (ok) {
								for(int i =0; i < sim.map.yellowpillars.size; i++) {
									sim.map.yellowpillars.get(i).state = VariableCollection.pillarMdown;
									sim.map.yellowpillars.get(i).activate();
								}
							}
						} else if( type == VariableCollection.button_blue) {
							boolean ok=true;
							for (int i = 0; i < sim.map.bluebuttons.size; i++){
								if (sim.map.bluebuttons.get(i).up)
									ok = false;}
							if (ok) {
								for(int i =0; i < sim.map.bluepillars.size; i++){
									sim.map.bluepillars.get(i).state = VariableCollection.pillarMdown;
									sim.map.bluepillars.get(i).activate();
								}
							}
						}
					}
				}
			}
		}
	}
	public void activate(){
		animTimer = 0;
		animStep = 0;
		animStep2 = 0;
	}

	public void render(SpriteBatch sb, float r, float b, float g, float a) {
		spriteToDraw.setColor(r,g,b,a);
		spriteToDraw.setPosition(spriteX, spriteY);
		spriteToDraw.draw(sb);

	}
	public void renderFlow(SpriteBatch sb, float r, float b, float g, float a) {
		if (hasFlower) { 
			StaticSprites.flower.setColor(r,g,b,a);
			StaticSprites.flower.setPosition(fxPos,fyPos);
			StaticSprites.flower.draw(sb);
		}
	}
	public void setState(int x) {
		state = x;
		animStep2 = 0;
		animStep = 0;
		animTimer = 0;
	}
	public String toString() {
		if (type == VariableCollection.hole)
			return "h";
		else if (type == VariableCollection.grass)
			return "g";
		else if (type == VariableCollection.dirt)
			return "d";
		else if (type == VariableCollection.pillar_blue)
			return "bp";
		else if (type == VariableCollection.pillar_yellow)
			return "yp";
		else if (type == VariableCollection.button_blue)
			return "bb";
		else if (type == VariableCollection.button)
			return "yb";
		return "g";
	}
}
